// app.js

import {URL, VERSION} from "./config/api.js"

import {Cache} from "./utils/cache.js"

const cache = new Cache()

App({
  onLaunch() {
  
    /**断网处理 */
    wx.onNetworkStatusChange((result) => {
      if(!result.isConnected) {
        wx.redirectTo({
          url: '/pages/error/error',
          success: (res) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
      }
    })
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId

        wx.request({
          url: URL + VERSION + "getOpenid",
          data:{code:res.code},
          method:"POST",
          success:res=>{
            if(res.data.code == 200) {
                cache.setCache('token',res.data.data.token)
            } else {
              wx.showToast({
                title: '登录失败',
              })
            }
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null
  }
})
